package com.irdstudio.cdp.pboc.service.impl;

import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.cdp.pboc.service.dao.ZxJsonInfoDao;
import com.irdstudio.cdp.pboc.service.domain.ZXJsonInfo;
import com.irdstudio.cdp.pboc.service.facade.ZXJsonInfoService;
import com.irdstudio.cdp.pboc.service.vo.ZXJsonInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @description:征信html服务
 * @date:2020年01月23日
 * @author:dengqiwen
 */
@Service("zxJsonInfoService")
public class ZXJsonInfoServiceImpl implements ZXJsonInfoService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(ZXJsonInfoServiceImpl.class);
	
	@Autowired
	private ZxJsonInfoDao zxJsonInfoDao;
	
	@Override
	public int insert(ZXJsonInfoVO vo) {
		if (logger.isDebugEnabled()) 
			logger.debug("当前新增数据为:"+ vo.toString());
		ZXJsonInfo zxJsonInfo = new ZXJsonInfo();
		int num = 0;
		try {
			beanCopy(vo, zxJsonInfo);
			num = zxJsonInfoDao.insert(zxJsonInfo);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
			throw e;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}


	@Override
	public ZXJsonInfoVO queryByPk(ZXJsonInfoVO vo) {
		logger.debug("当前查询参数信息为:"+ vo);
		try {
			ZXJsonInfo queryZXJsonInfo = new ZXJsonInfo();
			beanCopy(vo, queryZXJsonInfo);
			ZXJsonInfo queryZXJsonInfos = zxJsonInfoDao.queryByPk(queryZXJsonInfo);
			if (Objects.nonNull(queryZXJsonInfos)) {
				ZXJsonInfoVO outZXJsonInfoVO = beanCopy(queryZXJsonInfos, new ZXJsonInfoVO());
				logger.debug("当前查询结果为:"+ outZXJsonInfoVO.toString());
				return outZXJsonInfoVO;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
			throw e;
		}
		return null;
	}


	/**
	 * 更新操作
	 */
	@Override
	public int updateByPk(ZXJsonInfoVO vo)  throws Exception {
		logger.debug("当前修改数据为:"+ vo.toString());
		int num = 0;
		try {
			ZXJsonInfo zxJsonInfo = new ZXJsonInfo();
			beanCopy(vo, zxJsonInfo);
			num = zxJsonInfoDao.updateByPk(zxJsonInfo);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
			throw e;
		}
		logger.debug("根据条件:"+ vo +"修改的数据条数为"+ num);
		return num;
	}

	/**
	 * 功能描述:通过用户身份证号码查询所有征信报告
	 */
	@Override
	public List<ZXJsonInfoVO> queryReportId(ZXJsonInfoVO vo) throws Exception{
		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		ZXJsonInfo zxJsonInfo = new ZXJsonInfo();
		beanCopy(vo, zxJsonInfo);
		List<ZXJsonInfo> queryReports = zxJsonInfoDao.queryReportId(zxJsonInfo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ queryReports.size());
		List<ZXJsonInfoVO> list = null;
		try {
			list = (List<ZXJsonInfoVO>) beansCopy(queryReports, ZXJsonInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
			throw e;
		}
		return list;
	}

	/**
	 * 功能描述：通过征信报告id查询征信报告
	 */
	@Override
	public List<ZXJsonInfoVO> queryByReportId(ZXJsonInfoVO vo) throws Exception {

		logger.debug("当前查询本人征信报告信息为:");
		ZXJsonInfo zxJsonInfo = new ZXJsonInfo();
		beanCopy(vo, zxJsonInfo);
		List<ZXJsonInfo> queryReports = zxJsonInfoDao.queryByReportId(zxJsonInfo);
		logger.debug("当前查询本人数据信息的结果集数量为:"+ queryReports.size());
		List<ZXJsonInfoVO> list = null;
		try {
			list = (List<ZXJsonInfoVO>) beansCopy(queryReports, ZXJsonInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
			throw e;
		}
		return list;
	}


    @Override
    public List<ZXJsonInfoVO> queryByCertCodeAndCertType(ZXJsonInfoVO vo) throws Exception{
        logger.debug("当前查询参数信息为:"+ vo);
            ZXJsonInfo queryZXJsonInfo = new ZXJsonInfo();
            beanCopy(vo, queryZXJsonInfo);
            List<ZXJsonInfo> queryZXJsonInfos = zxJsonInfoDao.queryByCertCodeAndCertType(queryZXJsonInfo);
            logger.debug("当前查询结果集数量为:"+ queryZXJsonInfos.size());
			List<ZXJsonInfoVO> list;
		try {
			list = (List<ZXJsonInfoVO>) beansCopy(queryZXJsonInfos, ZXJsonInfoVO.class);
        } catch (Exception e) {
            logger.error("查询数据发生异常!", e);
            throw e;
        }
        return list;
    }

}
